package com.example.gameandroid;

import org.andengine.engine.Engine;
import org.andengine.entity.Entity;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.shape.IAreaShape;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.opengl.texture.region.ITextureRegion;

import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;

public class Mapa extends Entity{
	
	private Sprite mapa1;
	private IAreaShape rec;
	private Body bodyRec;
	private float height;
	
	public Mapa(Engine engine, float width, float height, ITextureRegion mapaTexture, PhysicsWorld physicsWorld) {
		this.height = height;
		
		mapa1 = new Sprite(0, 0, mapaTexture, engine.getVertexBufferObjectManager());
		
		attachChild(mapa1);
		
		rec = new Rectangle(0, 0, mapa1.getWidth(), 10, engine.getVertexBufferObjectManager());
		rec.setColor(1, 0, 0);
		
		
		attachChild(rec);
		rec.setVisible(false);
		
		FixtureDef f = PhysicsFactory.createFixtureDef(1, 0, 0);

		bodyRec = PhysicsFactory.createBoxBody(physicsWorld, rec, BodyType.StaticBody, f);
		bodyRec.setUserData("chao");
		
		physicsWorld.registerPhysicsConnector(new PhysicsConnector(rec, bodyRec));
	}
	
	public float getMapaWidth() {
		return mapa1.getWidth();	
	}
	
	@Override
	public void setPosition(float x, float y){
		mapa1.setPosition(x, y);
		bodyRec.setTransform((x + (rec.getWidth()/2))/32, ((y + height - rec.getHeight()) + (rec.getHeight() / 2) - 60)/32, bodyRec.getAngle());		
	}
	
	public Body getBody(){
		return bodyRec;
	}
	
	
}
